#!/usr/bin/env bash

echo -n "请输入 php 版本（默认7.4）：";
read php_v;

if [ ! -n "$php_v" ]; then
    php_v="7.4";
fi

file="/etc/php/${php_v}/fpm/php.ini";
echo -n "是否要备份 ${file} ？[Y/n]：";
read flag
case "$flag" in
    [yY][eE][sS]|[yY])
    #备份 php.ini
    bak="/tmp/php.ini.${php_v}.$(date "+%Y%m%d%H%M%S")";
    cp "$file" "$bak"
    echo "已备份至 ${bak}";
    ;;
    [nN][oO]|[nN])
    #不备份
    bak="";
    ;;
    *)
    echo "Input error, should be [Y/n].";
    exit 1;
esac

function replace_ini() {
    echo -n '原: '
    sudo sed -n "/^;\?${1} \?=.*/p" "$file"
    echo "新: ${1} = ${2}"
    sudo sed -in "s/^;\?${1} \?=.*/${1} = ${2}/" "$file"
    echo '---'
}

echo "配置替换开始...";
replace_ini max_execution_time 0
replace_ini max_input_time -1
replace_ini memory_limit 512M
replace_ini error_reporting E_ALL
replace_ini display_errors On
replace_ini display_startup_errors On
replace_ini post_max_size 100M
replace_ini upload_max_filesize 100M
replace_ini date.timezone PRC

sudo service "php${php_v}-fpm" restart
